package com.hyg.module_user.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.personal.UserDetail;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.module_user.R;
import com.hyg.lib_common.WebView.WebViewActivity;
import com.hyg.lib_common.constant.Constants;
import com.hyg.module_user.databinding.FragmentUserBinding;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 用户页
 */
public class UserFragment extends Fragment {
    FragmentUserBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentUserBinding.inflate(inflater);
        init();
        return binding.getRoot();
    }


    @Override
    public void onResume() {
        super.onResume();
        initImage();
    }

    public void init() {
        BaseActivity activity = (BaseActivity) getActivity();
        binding.myReport.setOnClickListener(new View.OnClickListener() {         //我的报告
            @Override
            public void onClick(View v) {
                activity.ToIntent(Constants.PATH_ACTIVITY_MODULE_REPORTS_ALLLIST, false);
                activity.overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });

        binding.myConstitutionList.setOnClickListener(new View.OnClickListener() {          //体质辨识报告
            @Override
            public void onClick(View v) {
                activity.ToIntent(Constants.PATH_ACTIVITY_MODULE_REPORTS_SELFTEST, false);
                activity.overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });

        binding.mySpiritList.setOnClickListener(new View.OnClickListener() {            //精神健康自检
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getActivity(), WebViewActivity.class);
                it.putExtra("url", UrlUtils.TEST_HISTORY + UserSPUtils.getUserInfoSp(getActivity()).getString("phone", "") + "&src=app&org=hyg");
                it.putExtra("title", "精神健康自检史");
                startActivity(it);
                getActivity().overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });

        binding.updateMyMessage.setOnClickListener(new View.OnClickListener() {          //完善个人信息
            @Override
            public void onClick(View v) {
                activity.ToIntent(Constants.PATH_ACTIVITY_MODULE_USER_SETTING_UPDATE, false);
                activity.overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });

        binding.systemSetup.setOnClickListener(new View.OnClickListener() {              //系统设置
            @Override
            public void onClick(View v) {
                activity.ToIntent(Constants.PATH_ACTIVITY_MODULE_USER_SYSTEMSETTING, false);
                activity.overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });
    }


    /**
     * description: 初始化用户信息，头像
     *
     * @author visitant
     * @time 2020/12/7 14:46
     */
    @SuppressLint("CheckResult")
    public void initImage() {
        Observable.just("")
                .map(new Function<String, UserDetail>() {
                    @Override
                    public UserDetail apply(String s) throws Exception {
                        UserDetail userDetail = UserSPUtils.getUserDetail(getActivity());
                        return (userDetail != null ? userDetail : new UserDetail());
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<UserDetail>() {
                    @Override
                    public void accept(UserDetail o) throws Exception {
                        updateImage(o);
                    }
                });
    }

    /**
     * description: 更新头像，完成度
     *
     * @author visitant
     * @time 2020/12/8 15:54
     */
    public void updateImage(UserDetail userDetail) {
        if (this.isRemoving() || binding==null) return;
        binding.messageProgress.setText("信息完善度 " + userDetail.getComputeProgress());
        binding.myName.setText(userDetail.getRealName());
        if (!Utils.isEmpty(userDetail.getHeadIcon())) {
            Glide.with(getActivity()).load(userDetail.getHeadIcon()).error(R.mipmap.headimg).into(binding.headImg);
        } else {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}